package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.constraintvalidators.bv.AssertFalseValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private AssertFalseValidator assertFalseValidator;

    //Request URL: http://localhost:8500/api/payment/wxPay/createJsapi/1301/312385026856
    @PostMapping("/createJsapi/{wxPayItemType}/{orderNo}")
    @TingshuLogin
    @Operation(summary = "预支付下单")
    public Result createJsapi(@PathVariable(value = "wxPayItemType") String wxPayItemType,
                              @PathVariable(value = "orderNo") String orderNo) {
        Map<String, Object> map = wxPayService.createJsapi(wxPayItemType, orderNo);
        return Result.ok(map);
    }

    //Request URL: http://localhost:8500/api/payment/wxPay/queryPayStatus/312385026856
    @GetMapping("/queryPayStatus/{orderNo}")
    @TingshuLogin
    @Operation(summary = "查询订单的支付状态")
    public Result queryPayStatus(@PathVariable(value = "orderNo") String orderNo) {
        Long userId = AuthContextHolder.getUserId();
        Transaction transaction = wxPayService.queryPayStatus(orderNo, userId);

        if (transaction != null && transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)) {
            //该笔订单支付成功[微信支付成功]

            wxPayService.paySuccess(orderNo, transaction);
            return Result.ok(true);
        } else {
            return Result.ok(false);
        }
    }

    // /api/payment/wxPay/notify
    @PostMapping("/notify")
    @Operation(summary = "异步通知支付结果")
    public Map asyncNotify(HttpServletRequest httpServletRequest) {
        Map<String, Object> map = new HashMap<>();
        //transaction:微信支付平台封装好的数据结果对象
        Transaction transaction = wxPayService.asyncNotify(httpServletRequest);
        if (transaction != null && transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)){
            wxPayService.paySuccess(transaction.getOutTradeNo(),transaction);
            map.put("code","SUCCESS");
            map.put("message","成功");
            return map;
        }
        map.put("code","FAIL");
        map.put("message","失败");
        return map;
    }
}
